package com.eascs.dubbo.api.crm;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.admin.api.loanapply.ApplForInquiryService;
import com.eascs.admin.o2o.internal.util.BaseUserUtil;
import com.eascs.admin.o2o.internal.util.StatusContexts;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oLoadUserZxServiceImpl;
import com.eascs.admin.o2o.o2ozx.serviceimpl.O2oZxServiceImpl;
import com.eascs.admin.o2o.o2ozx.trans.O2oUserZxTransFormart;
import com.eascs.admin.o2o.zx.entity.O2oLoanUserEntityZx;
import com.eascs.admin.o2o.zx.entity.O2oZxEntity;
import com.eascs.admin.o2o.zx.from.AddO2oZxForm;
import com.eascs.crm.common.custnew.entity.CustomerEntity;
import com.eascs.crm.common.custnew.service.CustomerService;
import com.eascs.crm.loanstart.service.LoanStartServiceImpl;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Service
public class ApplForInquiryServiceImpl implements ApplForInquiryService {
     
	@Reference
	CustomerService customerService;
	@Autowired
	LoanStartServiceImpl loanStartService;
	@Reference
	SysOrgService sysOrgService;
	
	@Autowired
	BaseUserUtil userUtils;
	
	@Autowired
	O2oZxServiceImpl o2oZxServiceImpl;
	@Autowired
	O2oLoadUserZxServiceImpl o2oLoadUserZxServiceImpl;
	@Override
	public CustomerEntity queryCustomerEntityByIdCard(String idCard) {
		return customerService.queryCustomerEntityByIdCard(idCard);
	}
	@Override
	public String saveOrupdateCustomerBasicInfo(AddO2oZxForm addO2oZxForm) {
		CustomerEntity customer = customerService.queryCustomerEntityByCustomerId(addO2oZxForm.getCustomerId());
		ActOrgEntity actOrgEntity = null;
		actOrgEntity = sysOrgService.getOrgByUnitNo(addO2oZxForm.getLtd());
		O2oZxEntity o2oZxEntity = new O2oZxEntity();
		
		if (!StatusContexts.O2O_BANKTYPE_BC.equals(addO2oZxForm.getZxBank())) {
			Boolean isMothSelected = o2oZxServiceImpl.isMothSelectedZX(customer.getIdNumber(), addO2oZxForm.getZxBank());
			if (isMothSelected) {
				return "N";
			}
		}
		o2oZxEntity.setCreateId(userUtils.getUser().getNo());
		o2oZxEntity.setCreateDate(new Date());
		o2oZxEntity.setCreateName(userUtils.getUser().getCname());
		o2oZxEntity.setIsDel(StatusContexts.O2O_NO);
		o2oZxEntity.setLtd(addO2oZxForm.getLtd());
		o2oZxEntity.setZxStatus(StatusContexts.O2O_NEW);
		o2oZxEntity.setLtdName(actOrgEntity.getOrgName());
		o2oZxEntity.setLtdCode(actOrgEntity.getUnitCode());
		o2oZxEntity.setOperatorId(addO2oZxForm.getOperatorNo());
		o2oZxEntity.setOperatorName(addO2oZxForm.getOperatorName());
		o2oZxEntity.setZxBank(addO2oZxForm.getZxBank());
		o2oZxEntity.setZxType(addO2oZxForm.getZxType());
		o2oZxEntity.setDataSource("3");
		o2oZxEntity.setBillCode(o2oZxServiceImpl.buildCode("O2OZX"));
		O2oLoanUserEntityZx o2oLoanUserEntity = O2oUserZxTransFormart.customerNewToloanUser(new O2oLoanUserEntityZx(), customer, o2oZxEntity.getId());
		return  o2oZxServiceImpl.saveNewEntityById(o2oZxEntity, o2oLoanUserEntity);	
	}

}
